<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商家名称" prop="merName">
        <el-input
            v-model="queryParams.merName"
            placeholder="请输入商家名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="shopName">
        <el-input
            v-model="queryParams.shopName"
            placeholder="请输入店铺名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:tshopaudit:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tshopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="商家名称" align="center" prop="merName"/>
      <el-table-column label="店铺名称" align="center" prop="shopName"/>
      <el-table-column label="详细地址" align="center" prop="addrDesc"/>
      <el-table-column label="客服电话" align="center" prop="serviceTel"/>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="t_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"  class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row, scope.index)"
                     v-hasPermi="['system:tmer:terminate']">查看
          </el-button>
          <el-button link type="primary" icon="Checked" @click="handleAudit(scope.row)"
                     v-hasPermi="['system:tshopaudit:audit']">审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 店铺审核 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="tmerRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核意见" prop="auditStatus">
              <el-radio-group v-model="form.auditStatus">
                <el-radio
                    v-for="dict in t_audit"
                    :key="dict.value"
                    :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核原因" prop="statusReason">
              <el-input v-model="form.statusReason" placeholder="请输入审核原因"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="auditSubmitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 查看商家详情 -->
    <el-dialog title="商家详情" v-model="viewOpen" width="1000px" append-to-body>
      <el-form ref="packageRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          
          <el-col :span="12">
            <el-form-item label="商家名称" prop="merId">
              <mer-select :key="refreshTime" v-model="form.merId" :typeList=[1,2]
                          :disabledValue="1"></mer-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="店铺名称" prop="shopName">
              {{  form.shopName  }}
              <!-- <el-input v-model="form.shopName" placeholder="请输入店铺名称" :disabled="true"/> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="店铺类型" prop="bizType">
              <el-select v-model="form.bizType" placeholder="请选择店铺类型" :disabled="true">
                <el-option
                    v-for="dict in shop_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营业时间" prop="bizHours">
              {{ form.bizHours }}
              <!-- <el-input v-model="form.bizHours" placeholder="请输入营业时间" :disabled="true"/> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客服电话" prop="serviceTel">
              <el-input v-model="form.serviceTel" placeholder="请输入客服电话" type="number" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地址" prop="addrDesc" label-width="auto">
              {{ form.addrDesc }}
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label="主图" prop="shopMainPic">
              <image-preview :src="form.shopMainPic" :width="150" :height="150"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
           
            <el-form-item label="详情图" prop="shopDetailPic">
              <image-preview :src-list="form.shopDetailPic" :width="150" :height="150"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="描述" prop="shopDesc">
              <editor v-model="form.shopDesc" :min-height="192" :readOnly="true"/>
            </el-form-item>
          </el-col>
        </el-row>
       
        
        
        
       
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="viewOpen = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Tshop">
import {listTshop, getTshop, updateStatus} from "@/api/system/tshop";
import {watch, onBeforeUnmount, ref} from "vue";
import useUserStore from "@/store/modules/user";

const {proxy} = getCurrentInstance();
const {shop_type, t_status, t_audit} = proxy.useDict('shop_type', 't_status', 't_audit');

const tshopList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const mapComponentKey = ref(0)
const viewOpen = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    merId: undefined,
    shopName: undefined,
    shopDesc: undefined,
    type: 1,
    bizHours: undefined,
    bizType: undefined,
    cityCode: undefined,
    areaCode: undefined,
    areaPath: undefined,
    addr: undefined,
    addrDesc: undefined,
    longitude: undefined,
    latitude: undefined,
    serviceTel: undefined,
    status: '01',
    statusReason: undefined,
  },
  rules: {
    auditStatus: [
      {required: true, message: "审核意见不能为空", trigger: "blur"}
    ],
    statusReason: [
      {required: true, message: "审核原因不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询店铺列表列表 */
function getList() {
  loading.value = true;
  listTshop(queryParams.value).then(response => {
    tshopList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    shopId: undefined,
    merId: undefined,
    shopName: undefined,
    shopMainPic: undefined,
    shopDetailPic: undefined,
    shopDesc: undefined,
    type: 1,
    bizHours: undefined,
    bizType: undefined,
    cityCode: undefined,
    areaCode: undefined,
    areaPath: undefined,
    addr: undefined,
    addrDesc: undefined,
    longitude: 118.8083,
    latitude: 31.8639,
    serviceTel: undefined,
    status: undefined,
    statusReason: undefined,
    auditTime: undefined,
    auditor: undefined,
    ext1: undefined,
    ext2: undefined,
    ext3: undefined,
    state: undefined,
    createTime: undefined,
    creator: undefined,
    modifyTime: undefined,
    modifier: undefined
  };
  mapComponentKey.value += 1;
  proxy.resetForm("tmerRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.shopId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/tshop/export', {
    ...queryParams.value
  }, `tshop_${new Date().getTime()}.xlsx`)
}

function handleCoordinateClicked(coordinate) {
  console.log(coordinate)
  // 更新经纬度
  form.value.longitude = coordinate.lng;
  form.value.latitude = coordinate.lat;
  form.value.addr = coordinate.addr;
}

function handleAudit(row) {
  reset();
  form.value = row;
  form.value.statusReason = ''
  open.value = true;
  title.value = "审核商家";
}

function auditSubmitForm(row) {
  proxy.$refs["tmerRef"].validate(valid => {
    if (valid) {
      if (form.value.merId != null) {
        form.value.status = form.value.auditStatus;
        updateStatus(form.value).then(response => {
          proxy.$modal.msgSuccess("审核成功");
          open.value = false;
          getList();
        });
      } else {

      }
    }
  });
}

/** 详细按钮操作 */
function handleView(row) {
  const _shopId = row.shopId || ids.value
  getTshop(_shopId).then(response => {
    response.data.shopDetailPic = response.data.shopDetailPic.split(',')
    form.value = response.data;
    viewOpen.value = true;
   
  });
}


onActivated(() => {
    getList();
})

getList();
const stopWatching = watch(() => useUserStore().cascaderKey, (v) => {
  getList();
}, {immediate: true});
onBeforeUnmount(() => {
  stopWatching();
});
</script>
